package com.student.controller;

import com.student.bean.SportIndex;
import com.student.bean.User;
import com.student.service.SportIndexService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class SportIndexController {
    @Resource
    private SportIndexService sportIndexService;

    //运动信息录入
    @ResponseBody
    @RequestMapping(value = "/insert")
    public boolean insertSportIndex(@RequestParam("run") Integer run,
                                    @RequestParam("rope") Integer rope,
                                    @RequestParam("foot") Integer foot,
                                    @RequestParam("situp") Integer situp,
                                    @RequestParam("pushup") Integer pushup,
                                    @RequestParam("plank") Integer plank,
                                    @RequestParam("date") String string) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = simpleDateFormat.parse(string);
        System.out.println("+++++   " +date);
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        Integer user_id = user.getUserid();
        return sportIndexService.insertSportIndex(run, rope, foot,
                situp, pushup, plank, date, user_id);
    }

    //运动信息查询
    @ResponseBody
    @RequestMapping(value = "/selectSportIndex")
    public List<SportIndex> selectSportIndex(@RequestParam("date") String string,
                                             HttpSession session) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = simpleDateFormat.parse(string);
        //将查询时间存入session
        session.setAttribute("date",date);
        return sportIndexService.selectSportIndex(date);
    }

    //运动信息修改跳转界面
    @RequestMapping(value = "/sportIndexSelectUpdate")
    public String sportIndexSelectUpdate() {
        return "sportIndexSelectUpdate";
    }

    //修改页面 显示原始数据
    @ResponseBody
    @RequestMapping(value = "/selectDate")
    public List<SportIndex> selectDate(HttpSession session)
            throws ParseException {
        Date date = (Date) session.getAttribute("date");
//        session.removeAttribute("date");
        System.out.println(" *** " + date);
        System.out.println(date);
        return sportIndexService.selectSportIndex(date);
    }

    //修改界面进行修改
    @ResponseBody
    @RequestMapping(value = "/updateSelect")
    public Boolean updateSelect(@RequestParam("run") Integer run,
                                @RequestParam("rope") Integer rope,
                                @RequestParam("foot") Integer foot,
                                @RequestParam("situp") Integer situp,
                                @RequestParam("pushup") Integer pushup,
                                @RequestParam("plank") Integer plank,
                                HttpSession session) {
        Date date = (Date) session.getAttribute("date");
        return sportIndexService.updateSelect(run, rope, foot, situp, pushup, plank, date);
    }

    //删除数据
    @ResponseBody
    @RequestMapping(value = "/deleteDate")
    public Boolean deleteDate(@RequestParam("date")String string) throws ParseException {
        System.out.println("---  " +string);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = simpleDateFormat.parse(string);
        System.out.println("+++ "+sportIndexService.deleteDate(date));
        System.out.println("*** "+date);
        return sportIndexService.deleteDate(date);
    }




}